package com.baoyouqun.controller.back;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.base.PageResponse;
import com.baoyouqun.domain.CreateCmd.UserPointsWriteOffCreateCmd;
import com.baoyouqun.domain.Query.UserPointsRecordQuery;
import com.baoyouqun.domain.UpdateCmd.UserPointsWriteOffUpdateCmd;
import com.baoyouqun.domain.VO.UserPointsRecordVO;
import com.baoyouqun.domain.VO.UserPointsVO;
import com.baoyouqun.domain.VO.UserVO;
import com.baoyouqun.service.UserPointsRecordService;
import com.baoyouqun.service.UserPointsService;
import com.baoyouqun.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 积分核销管理接口（后台专用）
 */
@RestController
@RequestMapping("/back/userPointsWriteOff")
@Api(tags = "积分核销管理接口")
public class UserPointsWriteOffController {

    @Resource
    private UserPointsRecordService userPointsRecordService;
    @Resource
    private UserPointsService userPointsService;
    @Resource
    private UserService userService;

    @GetMapping("/searchWithPoints")
    @ApiOperation(value = "根据姓名/手机号搜索用户及积分", notes = "用于“新增核销”弹窗的用户选择")
    public List<UserPointsVO> searchUsersWithPoints(
            @ApiParam(value = "搜索关键词（姓名或手机号，模糊匹配）", required = true)
            @RequestParam String keyword) {
        return userPointsService.listUsersByNameOrPhone(keyword);
    }

    /**
     * 1. 分页查询核销记录（适配页面列表）
     */
    @GetMapping("page")
    @ApiOperation(value = "分页查询核销记录", notes = "支持用户名、核销时间、操作人筛选，默认每页10条")
    public PageResponse<UserPointsRecordVO> pageWriteOff(
            @ApiParam(value = "核销查询条件", required = false)
            @ModelAttribute UserPointsRecordQuery query
    ) {
        IPage<UserPointsRecordVO> page = userPointsRecordService.pageWriteOffVo(query);
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }

    /**
     * 2. 创建积分核销记录（适配页面“新建核销”按钮）
     */
    @PostMapping("create")
    @ApiOperation(value = "创建积分核销", notes = "需传入用户ID、核销积分数（正数）、描述、操作人")
    public SingleResponse<UserPointsRecordVO> createWriteOff(
            @ApiParam(value = "核销创建参数", required = true)
            @Valid @RequestBody UserPointsWriteOffCreateCmd cmd
    ) {
        UserVO userVO = userService.selectById(StpUtil.getLoginIdAsString());
        cmd.setOperator(userVO.getName());
        return userPointsRecordService.createWriteOff(cmd);
    }

    /**
     * 3. 编辑积分核销记录（适配页面“编辑”按钮）
     */
    @PutMapping("update")
    @ApiOperation(value = "编辑积分核销", notes = "需传入核销ID、新核销积分数、新描述")
    public Response updateWriteOff(
            @ApiParam(value = "核销编辑参数", required = true)
            @Valid @RequestBody UserPointsWriteOffUpdateCmd cmd
    ) {
        return userPointsRecordService.updateWriteOff(cmd);
    }

    /**
     * 4. 删除积分核销记录（适配页面“删除”按钮）
     */
    @DeleteMapping("delete")
    @ApiOperation(value = "删除积分核销", notes = "传入核销记录ID，删除后自动恢复用户积分")
    public Response deleteWriteOff(
            @ApiParam(value = "核销记录ID", required = true)
            @RequestParam String id
    ) {
        StpUtil.checkRole("R010000");
        return userPointsRecordService.deleteWriteOff(id);
    }

    /**
     * 5. 导出核销记录Excel（适配页面“导出表格”按钮）
     */
    @PostMapping("export")
    @ApiOperation(value = "导出核销记录Excel", notes = "按当前筛选条件导出全量核销记录")
    public void exportWriteOff(
            @ApiParam(value = "核销查询条件（与分页一致）", required = false)
            @RequestBody(required = false) UserPointsRecordQuery query,
            @ApiParam(value = "HTTP响应流（用于下载）", hidden = true)
            HttpServletResponse response
    ) {
        userPointsRecordService.exportWriteOffExcel(query, response);
    }
}